import unittest, requests

class Buygoods(unittest.TestCase):

    token = None
    # 1、登录
    def test_01_login(self):
        login = requests.post(
            url='http://10.59.9.14:7003/api/public/login',
            json={"username":"15523615759","password":"123456Aa"}
        )
        print(login.json())
        Buygoods.token = login.json()['data']['token']
        # 断言
        self.assertEqual('ok', login.json()['info'])

    # 2、个人中心
    def test_02_info(self):
        info = requests.get(
            url='http://10.59.9.14:7003/api/ucenter/info',
            headers={'Auth-Token': 'Bearer '+Buygoods.token}
        )
        print(info.json())
        # 断言
        self.assertEqual('15523615759', info.json()['data']['username'])

     # 3、秒杀列表
    def test_03_second(self):
        second = requests.get(
            url='http://10.59.9.14:7003/api/second?page=1&type=0&sort=1',
        )
        print(second.json())
        # 断言
        self.assertIn('o', second.json()['info'])

    # 4、商品详情
    goodsid = None
    def test_04_view(self):
        view = requests.get(
            url='http://10.59.9.14:7003/api/second/view?id=14',
        )
        print(view.json())
        Buygoods.goodsid = view.json()['data']['goods']['id']
        # 断言
        self.assertIn('o', view.json()['info'])

    # 5、创建订单
    def test_05_confirm(self):
        confirm = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id='+str(Buygoods.goodsid)+'&type=second&sku_id=&num=1&shipping_type=1',
            headers={'Auth-Token': 'Bearer ' + Buygoods.token}
        )
        print(confirm.json())
        # 断言
        self.assertNotEquals('11', confirm.json()['data']['item'][0]['goods_id'])


    # 6、支付订单
    def test_06_create(self):
        create = requests.post(
            url='http://10.59.9.14:7003/api/order/create',
            headers={'Auth-Token': 'Bearer ' + Buygoods.token},
            json={"id":str(Buygoods.goodsid),"type":"second","address_id":278,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.14:7003/wap/pages/cart/confirm?id=14&sku_id=&num=1&type=second","sku_id":"","num":"1"}
        )
        print(create.json())
        # 断言
        self.assertEqual('支付成功', create.json()['data']['msg'])

if __name__ == '__main__':
    unittest.main()